/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package masterclient;

import Protocol.ClientObject;
import Protocol.ComProtocol;
import Protocol.ReturnObject;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Writer;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Erik
 */
public class DialoogQueryCreate extends JDialog implements ActionListener{
    
    private File file;
    private String path;
    private Writer writer = null;
    private ComProtocol cp;
    private String result = "";
    private ReturnObject ro;
    private ArrayList<String> stringList;
    
    //Variabelen van DialoogQueryCreate
    private JPanel panel;
    private JLabel labelName = new JLabel("Query name");
    private JLabel labelQuery = new JLabel("Enter query");
    private JTextField fieldName = new JTextField(1);
    private JTextField fieldQuery = new JTextField(1);
    private JTextField fieldTest = new JTextField(1);
    private JButton buttonCreate = new JButton("Create");
    private JButton buttonTest = new JButton("Test the query");
    
    //Constructor
    public DialoogQueryCreate(){
        //Standaard schermgegevens aanmaken
        setTitle("Create new query");
        setSize(320, 275);
        setBackground(Color.WHITE);
        setVisible(false);
        setResizable(false);
        
        //Toevoegen van de velden etc.
        createScreen();
    }
    
    //Methode om het scherm te maken
    public void createScreen(){
        //Eerst wordt een nieuw Jpanel aangemaakt, deze is zonder layout, alles wordt dus met coordinaten geplaatst
        panel = new JPanel();
        panel.setLayout(null);
        
        //Toevoegen labelName
        labelName.setBounds(10, 20, 70, 20);
        panel.add(labelName);
        
        //Toevoegen fieldName
        fieldName.setBounds(100, 20, 120, 20);
        panel.add(fieldName);
        
        //Toevoegen labelQuery
        labelQuery.setBounds(10, 50, 70, 20);
        panel.add(labelQuery);
        
        //Toevoegen fieldQuery
        fieldQuery.setBounds(10, 80, 300, 20);
        panel.add(fieldQuery);
        
        //Toevogen create button
        buttonCreate.setBounds(10, 110, 75, 20);
        panel.add(buttonCreate);
        
        //Toevogen test button
        buttonTest.setBounds(10, 160, 120, 20);
        panel.add(buttonTest);
        
        //Toevoegen test field
        fieldTest.setBounds(10, 190, 120, 20);
        fieldTest.setEnabled(false);
        fieldTest.setText("Niet getest");
        panel.add(fieldTest);
        
        //Voegt ActionListeners toe
        buttonCreate.addActionListener(this);
        
        //Zorgt ervoor dat het panel toegevoegd wordt aan het scherm
        add(panel);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == buttonCreate){
            if(fieldName.getText().equals("") || fieldQuery.getText().equals("")){
                popup("Niet alle velden zijn ingevuld.");
            }
            
            makeQuery(fieldName.getText(), fieldQuery.getText());
        }else if(e.getSource() == buttonTest){
            if(fieldQuery.getText().equals("")){
                popup("Geen query ingevuld.");
            }else{
                if(executeQuery(fieldQuery.getText())){
                    fieldTest.setText("Succesvol");
                }else{
                    fieldTest.setText("Onsuccesvol");
                }
            }
        }
    }
    
    public void makeQuery(String name, String query){
        String this_query = "INSERT INTO [Auth].[dbo].[Query]([Query], [Description], [isActive]) VALUES ('" + query + "', '" + name + "', " + "TRUE);";
        if(executeQuery(this_query)){
            popup("Query succesvol opgeslagen.");
        }else{
            popup("Fout bij opslaan query.");
        }
    }
    
    public boolean executeQuery(String executableQuery){
        boolean success = false;
        
        cp = new ComProtocol("localhost", 5555);
        ClientObject o = new ClientObject("system", "54b53072540eeeb8f8e9343e71f28176", cp.getUpdateQuery(), executableQuery);
        cp.send(o);
        ReturnObject ro = (ReturnObject) cp.recieve(); 
        cp.close();        
        if(ro != null){
            if(ro.getMessage().equals("ok")){        
                System.out.println("Query uitgevoerd.");
                success = true;
            }
        }else{
            System.out.println("Geen RO terug");
        }
        
        return success;
    }
    
    public JComboBox refreshElements(JComboBox cbox){
        cbox.removeAllItems();        
        String query = "SELECT [Description] FROM [Auth].[dbo].[Query];";
        
        cp = new ComProtocol("localhost", 5555);
        ClientObject o = new ClientObject("system", "54b53072540eeeb8f8e9343e71f28176", cp.getUpdateQuery(), query);
        cp.send(o);
        ReturnObject ro = (ReturnObject) cp.recieve();
        cp.close();
        
        if(ro != null){
            for(ArrayList a: ro.getRijen()){
                for(Object s: a){
                    cbox.addItem(s.toString());
                }
            }
        }        
        return cbox;
    }
    
    public void popup(String message){
        JOptionPane pane = new JOptionPane();
        pane.showMessageDialog(this, message);
    }
    
    public void activateQuery(String queryName){
        String query = "SELECT [isActive] FROM [Auth].[dbo].[Query] WHERE [Description]='" + queryName + "' AND isActive=TRUE;";
        String newState;
        
        cp = new ComProtocol("localhost", 5555);
        ClientObject o = new ClientObject("system", "54b53072540eeeb8f8e9343e71f28176", cp.getUpdateQuery(), query);
        cp.send(o);
        ReturnObject ro = (ReturnObject) cp.recieve();
        cp.close();
        
        if(ro != null){
            newState = "FALSE";
        }else{
            newState = "TRUE";
        }
        
        query = "UPDATE [Auth].[dbo].[Query] SET [isActive]='" + newState + "' WHERE [Description]='" + queryName + "';";
        if(!executeQuery(query)){
            popup("Veranderen status niet gelukt.");
        }
    }
    
    public void deleteQuery(String queryName){
        String query = "DELETE * FROM [Auth].[dbo].[Query] WHERE [Description]='" + queryName + "';";
        if(!executeQuery(query)){
            popup("Verwijderen query niet gelukt.");
        }
    }
}